/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package webprocessors;

import containers.TestScores;
import java.io.IOException;
import java.util.LinkedList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import readers.QueryStringHandler;
import readers.UserReader;

/**
 *
 * @author Chris
 */
public class webprocessorTest extends webprocessor {
    public webprocessorTest() {}
    
    @Override
    public String process(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        QueryStringHandler QSH = new QueryStringHandler(request.getQueryString());
        String user = getUserName(request);
        
        String tid = QSH.getElement("tid");
        String groupid = QSH.getElement("grpid");
        LinkedList<TestScores> scores = new LinkedList<TestScores>();
        
        if(tid == null || groupid == null) {
            return "An error has occured";
        }

        if(!UserReader.getTestScores(user, groupid, tid, scores)) {
            return "This test hasn't been completed by any students";
        }
        
        TestScores TS;
        String output = "<table><tr><td>Student</td><td>Score</td></tr>";  
        while(!scores.isEmpty()) {
            TS = scores.removeFirst();
            output += "<tr><td>" + TS.userName + "</td><td>" + 
                    TS.score + "</td></tr>";
        }
        
        output += "</table>";
        
        return output;
    }

}
